/*
 * @(#)EmailMessageSenderComparator.java
 *
 * Copyright (c) 2004 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */

package com.dcivision.upgrade;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dcivision.framework.DataSourceFactory;
import com.dcivision.upgrade.handler.LuceneIndexUpgradeHandler;
import com.dcivision.upgrade.handler.UpgradeHandler;
import com.dcivision.upgrade.handler.UpgradeHandler4Application319To320;
import com.dcivision.upgrade.handler.UpgradeHandler4DMSModule319To320;
import com.dcivision.upgrade.handler.UpgradeHandler4FormModule319To320;
import com.dcivision.upgrade.handler.UpgradeHandler4SystemSetup319To320;
import com.dcivision.upgrade.handler.UpgradeHandler4WorkflowModule319To320;

/**
 * Managing all versions of system upgrade.
 * 
 * @author      henry.chan
 * @author      Brook.Huang
 * @company     DCIVision Limited
 * @creation    19/04/2007
 * @version     $Revision: 1.7.2.4 $
 */
public class UpgradeManager {

  private static final Log LOG = LogFactory.getLog(UpgradeManager.class);

  public static UpgradeHandler getHandlerFor319To320(UpgradeRequest upgradeRequest) throws Exception {

    if (upgradeRequest == null || upgradeRequest.getUpgradeParameter("oldLuceneIndexPath") == null
        || upgradeRequest.getUpgradeParameter("isUpdateFromOldLuceneIndex") == null) {
      throw (new IllegalArgumentException("Illegal upgrade request from 319 to 320!"));
    }

    LOG.info("oldLuceneIndexPath : " + upgradeRequest.getUpgradeParameter("oldLuceneIndexPath") + "\n" + "isUpdateFromOldLuceneIndex : "
        + upgradeRequest.getUpgradeParameter("isUpdateFromOldLuceneIndex") + "\n");

    UpgradeHandler applicationUpgradeHandler = null;

    UpgradeContext upgradeContext = new StandardUpgradeContext(DataSourceFactory.getConnection(), upgradeRequest, true);

    // Construct all upgrade handler.
    applicationUpgradeHandler = new UpgradeHandler4Application319To320(upgradeContext);
    applicationUpgradeHandler.addUpgradeExceptionHandler(new GeneralExceptionHandler());

    applicationUpgradeHandler.addHandler(new UpgradeHandler4SystemSetup319To320(upgradeContext));
    applicationUpgradeHandler.addHandler(new UpgradeHandler4DMSModule319To320(upgradeContext));
    applicationUpgradeHandler.addHandler(new UpgradeHandler4FormModule319To320(upgradeContext));
    applicationUpgradeHandler.addHandler(new UpgradeHandler4WorkflowModule319To320(upgradeContext));

    upgradeContext.setTopUpgradeHandler(applicationUpgradeHandler);

    return (applicationUpgradeHandler);
  }
  
  public static UpgradeHandler getHandler4RebuildLuceneIndex(UpgradeRequest rebuildRequest) throws Exception {
    
    UpgradeContext upgradeContext = new StandardUpgradeContext(DataSourceFactory.getConnection(), rebuildRequest, true);
    UpgradeHandler rebuildLuceneIndexHandler = new LuceneIndexUpgradeHandler(upgradeContext);
    rebuildLuceneIndexHandler.addUpgradeExceptionHandler(new GeneralExceptionHandler());
    
    upgradeContext.setTopUpgradeHandler(rebuildLuceneIndexHandler);
    
    return rebuildLuceneIndexHandler;
  }

}
